#!/usr/bin/env python
#coding: utf-8

################################
# file: crawler.py             #
# function:                    #
# author: xsseroot@gmail.com   #
# time: 2014-9-1 21:02:27      #
################################
#
#由于http://www.22mm.cc/目标网站使用安全狗软件.
#添加 -m参数，是否启用多线程
#

import sys
import os

reload(sys)
sys.setdefaultencoding('utf-8')
sys.path.append(os.getcwd())
from lib.core.log import logger as log
from lib.uitls.crawler import Crawl
from lib.parse.xmlproc import XmlLoader

def main():
    threadCount = 5
    savePath = "%s/pics" % os.getcwd()
    limitPicCount = -1
    enabledMultithreading = False
    
    program = None
    if len(sys.argv) == 2:
        if sys.argv[1] == "-h":
            print '''
-------------------------------------------------------------
This program is crawling MM pictures. Version beta 1.0
by: xsseroot@gamil.com time: 2014-09
-------------------------------------------------------------
Option:
    -v : Prints the version number  
    -h : Display this help
    -n : The number of threads, the default is 10.
    -o : Crawl save path, the default is "./pics/" path.
    -l : Set the number of crawling pictures, the default is 0 not limit. 
    -m : Multithreading is enabled.

Demo:
-------------------------------------------------------------
    mm_crawler.py -n5 -o"d:/pics" -l20
    mm_crawler.py -n5
    mm_crawler.py -l15
    mm_crawler.py -m'''
    
            sys.exit()
			
        if sys.argv[1] == "-v":
            print "Version beta 2.0"
            sys.exit()

    for currParaIndex in range(1,len(sys.argv)):
        try:
            if sys.argv[currParaIndex].startswith("-"):
                option = sys.argv[currParaIndex][1:2]
                value = sys.argv[currParaIndex][2:]
                if option == "n":
                    threadCount = int(value)
                if option == "o":
                    savePath = value
                if option == "l":
                    limitPicCount = int(value)
                if option == "m":
                    enabledMultithreading = True
        except ValueError,Msg:
            log.warning((ValueError,Msg))
        except NameError,Msg:	
            log.error((NameError,Msg))
            sys.exit()
		
    # statrt execute
    log.debug("Current thread count:%s" % threadCount )
    log.debug("Output path:%s" % savePath )
    log.debug("limit pictures count:%s" % limitPicCount )
    columnlist = XmlLoader.getColumns('./conf/mm_crawler.conf')
    crawl = Crawl(columnlist, savePath, limitPicCount, threadCount, enabledMultithreading)
    crawl.run()
    
if __name__ == "__main__":
	main()